package com.hqq.web.page;

import com.alibaba.fastjson.JSONObject;
import com.hqq.domain.WoocCard;
import com.hqq.domain.WoocCardDetail;
import com.hqq.domain.WoocUser;
import com.hqq.enums.ResultEnum;
import com.hqq.service.WoocCardService;
import com.hqq.util.Response;
import com.hqq.vo.CardDetailVO;
import com.hqq.vo.CardRemarkVO;
import com.hqq.vo.CardVO;
import com.hqq.web.consts.WoocConsts;
import com.hqq.web.util.CookieUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * CardController
 * Created by heqianqian on 2018/3/26.
 */
@Controller
@RequestMapping("/card")
public class CardController {
    private static final Logger logger = LoggerFactory.getLogger(CardController.class);

    @Autowired
    private WoocCardService woocCardService;

    /*=============页面相关===========*/

    /**
     * 跳转到论坛首页
     *
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request) {
        try {
            String userInfo = CookieUtil.findCookie(request, WoocConsts.COOKIE_KEY);
            if (StringUtils.isNotBlank(userInfo)) {
                WoocUser user = JSONObject.parseObject(userInfo, WoocUser.class);
                if (null != user) {
                    model.addAttribute("name", user.getUserName());
                }
            }
        } catch (Exception e) {
            logger.error("===CardController index===", e);
            return "error";
        }
        return "card/index";
    }

    /**
     * 跳转到列表详情页
     *
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Long cardId, Model model, HttpServletRequest request) {
        try {
            String userInfo = CookieUtil.findCookie(request, WoocConsts.COOKIE_KEY);
            if (StringUtils.isNotBlank(userInfo)) {
                WoocUser user = JSONObject.parseObject(userInfo, WoocUser.class);
                if (null != user) {
                    model.addAttribute("name", user.getUserName());
                }
            }
            logger.debug("===CardController detail===cardId:{}", cardId);
            model.addAttribute("cardId", cardId);
        } catch (Exception e) {
            logger.error("===CardController index===", e);
            return "error";
        }
        return "card/detail";
    }


    /**
     * 跳转到发帖页面
     *
     * @return
     */
    @RequestMapping("/add")
    public String add() {
        return "card/add_card";
    }

    /*=============接口相关===========*/

    /**
     * 获取帖子列表信息
     *
     * @param sortType 排序类型
     * @param typeId   资源类型
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_card_list.ajax")
    public Response getCardList(Long sortType, Long typeId) {
        logger.debug("===CardController getCardList===sortType:{},typeId:{}", sortType, typeId);
        Response response = null;
        try {
            List<CardVO> cardVOS = woocCardService.listCards(sortType, typeId);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), cardVOS);
        } catch (Exception e) {
            logger.error("===CardController getCardList===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 查询单个帖子的详细信息
     *
     * @param cardId
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_card_detail.ajax")
    public Response getCardDetail(Long cardId) {
        logger.debug("===CardController getCardDetail===cardId:{}", cardId);
        Response response = null;
        try {
            CardDetailVO detailVO = woocCardService.getCardDetail(cardId);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), detailVO);
        } catch (Exception e) {
            logger.error("===CardController getCardDetail===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 发表帖子
     *
     * @param userId
     * @param cardTitle
     * @param cardContent
     * @param cardTypeId
     * @return
     */
    @ResponseBody
    @RequestMapping("/publish_card.ajax")
    public Response publishCard(Long userId, String cardTitle, String cardContent, Long cardTypeId) {
        logger.debug("===CardController publishCard===userId:{},cardTitle:{},cardContent:{},cardTypeId:{}", userId, cardTitle, cardContent, cardTypeId);
        Response response = null;
        try {
            WoocCard woocCard = woocCardService.publishCard(userId, cardTitle, cardContent, cardTypeId);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), woocCard);
        } catch (Exception e) {
            logger.error("===CardController getCardDetail===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }


    /**
     * 发表评论
     *
     * @param userId
     * @param cardId
     * @param remarkContent
     * @return
     */
    @ResponseBody
    @RequestMapping("/publish_remark.ajax")
    public Response publishRemark(Long userId, Long cardId, String remarkContent) {
        logger.debug("===CardController publishRemark===userId:{},cardId:{},remarkContent:{}", userId, cardId, remarkContent);
        Response response = null;
        try {
            CardRemarkVO remarkVO = woocCardService.publishRemark(userId, cardId, remarkContent);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), remarkVO);
        } catch (Exception e) {
            logger.error("===CardController getCardDetail===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 删除发帖
     *
     * @param cardId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete_card.ajax")
    public Response deleteCard(Long cardId) {
        logger.debug("===CardController deleteCard===cardId:{}", cardId);
        Response response = null;
        try {
            woocCardService.deleteCard(cardId);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription());
        } catch (Exception e) {
            logger.error("===CardController deleteCard===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 删除发帖
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/add_card.ajax")
    public Response addCard(String cardTitle, String cardContent, Long typeId, HttpServletRequest request) {
        logger.debug("===CardController deleteCard===cardTitle:{},cardContent:{},typeId:{}", cardTitle, cardContent, typeId);
        Response response = null;
        try {
            String userInfo = CookieUtil.findCookie(request, WoocConsts.COOKIE_KEY);
            if (StringUtils.isNotEmpty(userInfo)) {
                WoocUser woocUser = JSONObject.parseObject(userInfo, WoocUser.class);
                if (woocUser != null) {
                    woocCardService.addCard(cardTitle, cardContent, typeId, woocUser.getUserId());
                    response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription());
                    return response;
                }
            }
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        } catch (Exception e) {
            logger.error("===CardController deleteCard===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

}
